package dao;

import db.DBUtil;
import model.Goddess;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoddessDao {


    public List<Goddess> query() {
        List<Goddess> list = new ArrayList<>();
        try {
            Connection conn = DBUtil.getConn();
            Statement stmt = null;
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from imooc_goddess");
            while (rs.next()) {
                Goddess goddess = new Goddess();
                goddess.setUserName(rs.getString("user_name"));
                goddess.setAge(rs.getInt("age"));
                list.add(goddess);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }


    public List<Goddess> query(List<Map<String,Object> >parmas) {
        List<Goddess> list = new ArrayList<>();
        try {
            Connection conn = DBUtil.getConn();
            StringBuilder sql = new StringBuilder();
            sql.append("select * from imooc_goddess where 1 = 1 ");
            if(parmas!=null){
                for (Map<String, Object> map : parmas) {
                    sql.append("and " + map.get("name") + map.get("rela") + map.get("value"));
                }
            }
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            //System.out.println(sql.toString());
            ResultSet resultSet = stmt.executeQuery();
            while (resultSet.next()) {
                Goddess goddess = new Goddess();
                goddess.setId(resultSet.getInt("id"));
                goddess.setUserName(resultSet.getString("user_name"));
                goddess.setSex(resultSet.getInt("sex"));
                goddess.setAge(resultSet.getInt("age"));
                goddess.setEmail(resultSet.getString("email"));
                goddess.setMobile(resultSet.getString("mobile"));
                list.add(goddess);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }



    public boolean addGoddess(Goddess goddess) {
        try {
            Connection conn = DBUtil.getConn();
            String sql = ""+
                    " insert into imooc_goddess"+
                    " (user_name,sex,age,email,mobile)"+
                    " values(?,?,?,?,?)";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,goddess.getUserName());
            preparedStatement.setInt(2,goddess.getSex());
            preparedStatement.setInt(3,goddess.getAge());
            preparedStatement.setString(4,goddess.getEmail());
            preparedStatement.setString(5,goddess.getMobile());
            preparedStatement.execute();
            return true;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateGoddess(Goddess goddess){
        try {
            Connection conn = DBUtil.getConn();
            String sql = "update"+
                    " imooc_goddess"+
                    " set user_name = ?,sex = ?,age = ?,email = ?,mobile = ?"+
                    " where id = ?";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,goddess.getUserName());
            preparedStatement.setInt(2,goddess.getSex());
            preparedStatement.setInt(3,goddess.getAge());
            preparedStatement.setString(4,goddess.getEmail());
            preparedStatement.setString(5,goddess.getMobile());
            preparedStatement.setInt(6,goddess.getId());
            preparedStatement.execute();
            return true;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteGoddess(int id){
        try {
            Connection conn = DBUtil.getConn();
            String sql = "delete"+
                    " imooc_goddess"+
                    " where id = ?";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.execute();
            return true;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }


    public Goddess get(int id){
        Goddess goddess = null;
        try {
            Connection conn = DBUtil.getConn();
            String sql = "select * from"+
                    " imooc_goddess"+
                    " where id = ?";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            ResultSet resultSet =  preparedStatement.executeQuery();

            while (resultSet.next()){
                goddess = new Goddess();
                goddess.setId(resultSet.getInt("id"));
                goddess.setUserName(resultSet.getString("user_name"));
                goddess.setSex(resultSet.getInt("sex"));
                goddess.setAge(resultSet.getInt("age"));
                goddess.setEmail(resultSet.getString("email"));
                goddess.setMobile(resultSet.getString("mobile"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return goddess;
    }

}
